$(function() {

	var setOptionData = {
		title: {
			text: '功率vs辐照',
			textStyle: {
				color: '#fefefe',
				fontSize: 16
			},
			top: '3%'
		},
		tooltip: {
			trigger: 'axis'
		},
		legend: {
			top: '3%',
			right: '3%',
			data: ['功率', '辐照'],
			textStyle: {
				color: '#fefefe'
			}
		},
		grid: {
			left: '3%',
			right: '4%',
			bottom: '5%',
			top: '22%',
			containLabel: true
		},
		xAxis: {
			name: '/h',
			data: creatAxis(),
			// splitNumber: 24, //直接受区域大小影响，自动生成了12格
			axisLine: {
				lineStyle: {
					//x轴的坐标轴颜色
					color: '#b9d5ff'
				}
			},
			nameTextStyle: {
				//x轴的标题颜色
				color: '#b9d5ff'
			}
		},
		yAxis: [{
			type: 'value',
			name: '功率(kW)',
			min: 0,
			max: 350,
			interval: 50,
			splitNumber: 7,
			splitLine: {
				//显示y轴的分割线
				lineStyle: {
					//y轴的分割线颜色
					color: 'rgba(185,213,255,0)'
				}
			},
			axisLine: {
				lineStyle: {
					//y轴的坐标轴颜色
					color: '#b9d5ff'
				}
			},
			nameTextStyle: {
				//y轴标题的颜色
				color: '#b9d5ff'
			}
		}, {
			type: 'value',
			name: '辐照(W/㎡)',
			min: 0,
			max: 350,
			interval: 50,
			splitNumber: 7,
			splitLine: {
				//显示y轴的分割线
				lineStyle: {
					//y轴的分割线颜色
					color: 'rgba(185,213,255,0)'
				}
			},
			axisLine: {
				lineStyle: {
					//y轴的坐标轴颜色
					color: '#b9d5ff'
				}
			},
			nameTextStyle: {
				//y轴标题的颜色
				color: '#b9d5ff'
			}
		}],
		series: [{
			name: '功率',
			type: 'line',
			data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
			],
			itemStyle: {
				normal: {
					color: '#61d13e'
				}
			}
		}, {
			name: '辐照',
			type: 'line',
			yAxisIndex: 1,
			data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
			],
			itemStyle: {
				normal: {
					color: '#fe6c26'
				}
			}
		}]
	};

	// 创建坐标X轴
	function creatAxis() {
		var arr = [];
		var s;
		for(var i = 0; i < 24; i++) {
			for(var j = 0; j < 6; j++) {
				if(j == 0) {
					s = String(i);
				} else {
					s = i + ':' + j + '0';
				}
				arr.push(s);
			}
		}
		return arr;
	}

	//生成一个空数组
	function generateArr(count){
		 var array = new Array(count);
		 for(var i=0;i<array.length;i++){
			 array[i]=null;
		 }
		 return array;
	}
	
	function refreshData() {
		$.ajax({
			type: "post",
			url: "http://192.168.1.196:8088/ReesolaLocal/web/getPowerAndIrradiation",
			async: true,
			success: function(data) {
				// 请求成功执行代码     
				var interval = 10 * 60 * 1000;
				var now = (new Date()).Format("yyyy-MM-dd");
				var start = getDate(now).getTime();
				var end = start + 1000 * 60 * 60 * 24;
				var count = (end - start) / interval;

				try {
					var data = JSON.parse(data);
					if(data && data.length > 0) {
						var power = data[0].power;
						if(power && power.length > 0) {
							var powerlen = power.length;
							var arrpower = generateArr(count);
							for(var i = 0; i < powerlen; i++) {
								var time = power[i].index;
								var value = power[i].value;
								var timekey = getDate(time).getTime();
								var indexkey = getPostion(timekey, start, end, interval);
								arrpower[indexkey] = value;
							}
							
							var powermax = getYMax(arrpower, 7);
							
							if(powermax > 0) {
								myChart.setOption({
									yAxis: [{
										name: '功率(kW)',
										max: powermax,
										interval: powermax / 7
									}],
									series: [{
										name: '功率',
										type: 'line',
										data: arrpower
									}]
								})
							}
							
						}

						var irradiation = data[0].irradiation;
						if(irradiation && irradiation.length > 0) {
							var irrlen = irradiation.length;
							var arrirr = generateArr(count);
							for(var i = 0; i < irrlen; i++) {
								var time = irradiation[i].index;
								var value = irradiation[i].value;
								var timekey = getDate(time).getTime();
								var indexkey = getPostion(timekey, start, end, interval);
								arrirr[indexkey] = value;
							}
							
							var irrmax = getYMax(arrirr, 7);
							if(irrmax > 0){
								myChart.setOption({
									yAxis: [{
										name: '辐照(W/㎡)',
										max: irrmax,
										interval: irrmax / 7
									}],
									series: [{
										name: '辐照',
										type: 'line',
										yAxisIndex: 1,
										data: arrirr
									}]
								})
							
							}
						}
						
					}// if

				} catch(error) {
					console.log("数据解析失败！");
				}

			},
			error: function() {
				alert("数据调取失败！");
			}
		});
	}

	// 初始化图表
	var myChart = echarts.init(document.getElementById('echartBox'));
	myChart.setOption(setOptionData);

	// 执行函数，得到数据
	refreshData();
	
	// 刷新函数
	setInterval(function(){
		refreshData();
	},5000);

})